﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;

namespace TMDT_Nhom1A.San_Pham
{
    public partial class DanhSachSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ParentCatalogueID"] != null)
                {
                    int ParentCatalogue = int.Parse(Request.QueryString["ParentCatalogueID"]);
                    dllistproducts.DataSource = ElectricDevice_BUS.ListProductByParentCatalogue(ParentCatalogue);
                    dllistproducts.DataBind();
                }
                if (Request.QueryString["ChildCatalogueID"] != null)
                {
                    int ChildCatalogue = int.Parse(Request.QueryString["ChildCatalogueID"]);
                    dllistproducts.DataSource = ElectricDevice_BUS.ListProductByChildCatalogue(ChildCatalogue);
                    dllistproducts.DataBind();
                }
                lbCatalogue.Text = Request.QueryString["Name"];
            }

        }

        protected void dllistproducts_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "lbLinkToDetail" || e.CommandName == "ibLinkToDetail")
            {
                string url = "ChiTietSanPham.aspx";
                if (Request.QueryString["ChildCatalogueID"] != null)
                {
                    url = url + "?ChildCatalogueID=" + Request.QueryString["ChildCatalogueID"].ToString() + "&Name=" + Request.QueryString["Name"].ToString() + "&ElectricDeviceID=" + e.CommandArgument;
                    Response.Redirect(url);
                }
                if (Request.QueryString["ParentCatalogueID"] != null)
                {
                    url = url + "?ParentCatalogueID=" + Request.QueryString["ParentCatalogueID"].ToString() + "&Name=" + Request.QueryString["Name"].ToString() + "&ElectricDeviceID=" + e.CommandArgument;
                    Response.Redirect(url);
                }
            }
            if (e.CommandName == "AddToCart")
            {
                TextBox tbquantity = (TextBox)e.Item.FindControl("tbquantity");
                if (tbquantity.Text != "")
                {
                    MyOrderProduct_Online ItemCart = new MyOrderProduct_Online();
                    LinkButton lbLinkToDetail = (LinkButton)e.Item.FindControl("lbLinkToDetail");
                    ItemCart.ElectricDeviceID = lbLinkToDetail.CommandArgument.ToString();
                    Label lbRetailPrice = (Label)e.Item.FindControl("lbRetailPrice");
                    ItemCart.RetailPrice = decimal.Parse(lbRetailPrice.Text.ToString());
                    Label lbDealerPrice = (Label)e.Item.FindControl("lbDealerPrice");
                    ItemCart.DealerPrice = decimal.Parse(lbDealerPrice.Text.ToString());
                    Label lbNonDiscountPrice = (Label)e.Item.FindControl("lbNonDiscountPrice");
                    if (lbNonDiscountPrice.Text != "")
                        ItemCart.PriceCoupon = decimal.Parse(lbNonDiscountPrice.Text.ToString());

                    ItemCart.Quantity = int.Parse(tbquantity.Text);
                    Cart cart = (Cart)Session["Cart"];
                    cart.AddToCart(ItemCart);
                    Session["TotalPrice"] = cart.CalulateTotalPrice();
                    if (Session["Role"].ToString() == "User")
                    {
                        //CHI_TIET_DAT_HANG chitiet = new CHI_TIET_DAT_HANG();
                        //chitiet.Ms = int.Parse(masach.Text);
                        //chitiet.Ten_Sach = tensach.Text;
                        //chitiet.So_Luong = 1;
                        //chitiet.Thanh_Tien = Convert.ToDecimal(dongia.Text);
                        //chitiet.Don_Gia = Convert.ToDecimal(dongia.Text);
                        //User_BUS.LuuGioHang(int.Parse(Session["UserID"].ToString()), chitiet);
                    }
                    HyperLink hlTotalPrice = (HyperLink)GioHang1.FindControl("hlTotalPrice");
                    hlTotalPrice.Text = Session["TotalPrice"].ToString();
                    tbquantity.Text = "";
                }
            }
        }

        protected void dllistproducts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                string NonDiscountPrice = "";
                string DiscountPrice = "";
                LinkButton ID = (LinkButton)e.Item.FindControl("lbLinkToDetail");
                Coupon_BUS.GetPriceCouponOfElectricDevice(ID.CommandArgument, ref NonDiscountPrice, ref DiscountPrice);
                if (NonDiscountPrice != "" && DiscountPrice != "")
                {
                    e.Item.FindControl("del").Visible = true;
                    e.Item.FindControl("nondel").Visible = false;
                    Label lbDiscountPrice = (Label)e.Item.FindControl("lbDiscountPrice");
                    Label lbNonDiscountPrice = (Label)e.Item.FindControl("lbNonDiscountPrice");
                    lbDiscountPrice.Text = DiscountPrice;
                    lbNonDiscountPrice.Text = NonDiscountPrice;
                    e.Item.FindControl("pPriceCoupon").Visible = true;
                    e.Item.FindControl("pNonDiscountPrice").Visible = true;
                }
                if (NonDiscountPrice == "" && DiscountPrice == "")
                {
                    e.Item.FindControl("del").Visible = false;
                    e.Item.FindControl("nondel").Visible = true;
                }
            }
        }
    }
}
